/*
 * @Description:
 * @Version: 2.0
 * @Autor: snzo.chen
 * @Date: 2023-11-16 14:05:25
 * @LastEditors: snzo.chen
 * @LastEditTime: 2023-12-06 11:40:39
 */
import { App } from 'vue';
import { RouteRecordRaw, createRouter, createWebHashHistory } from 'vue-router';
import { createRouterGuards } from './router-guard';
import outsideLayout from './outsideLayout';
import { whiteNameList } from './constant';
import common from './staticModules';

export const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Layout',
    redirect: '/welcome',
    component: () => import('../layout/index.vue'),
    meta: {
      title: '首页',
    },
    children: [...common],
  },
  ...outsideLayout,
];

export const router = createRouter({
  history: createWebHashHistory(''),
  routes,
});

export function resetRouter() {
  router.getRoutes().forEach((route) => {
    const { name } = route;
    if (name && !whiteNameList.some((n) => n === name)) {
      router.hasRoute(name) && router.removeRoute(name);
    }
  });
}

export async function setupRouter(app: App) {
  createRouterGuards(router, whiteNameList);
  app.use(router);
  await router.isReady();
}

export default router;
